import java.awt.BorderLayout;
import java.awt.ComponentOrientation;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;

import java.io.*;
import java.sql.*;

/**
 * 
 */

/**
 * @author pt186002
 *
 */
public class AttendanceReportMainScreen extends JFrame implements ActionListener{
	
	private JComboBox m_month, m_year, m_reportType;
	private JButton btn_reset,btn_report;
	private JPanel m_centralPanel,m_leftPanel,m_firstPanel;
	
	
	public AttendanceReportMainScreen()
	{
		super( "Report generation" );
		
	}
	
	public void startScreen()
	{
		FlowLayout flowLayout = new FlowLayout(FlowLayout.CENTER);
		
		//Set frame properties
		setLayout(flowLayout);
		setResizable(false);
		
		//Create Panel
		m_centralPanel = new JPanel();
		m_leftPanel=new JPanel();
		m_firstPanel=new JPanel();
		
		//Add panel to frame
		add(m_firstPanel);
		add(m_centralPanel);
		add(m_leftPanel);
		
		//Set panel borders
		m_firstPanel.setBorder(BorderFactory.createTitledBorder("Report Type"));
        m_centralPanel.setBorder(BorderFactory.createTitledBorder("Month and year"));
        m_leftPanel.setBorder(BorderFactory.createTitledBorder(""));
		
    	//DropDownList for ReportType
        m_firstPanel.add(new JLabel(" Select Type of Report : "));
        m_reportType=new JComboBox();
        m_reportType.addItem("");
        m_reportType.addItem("Working Details");
        m_reportType.addItem("Leave Details");
        m_firstPanel.add( m_reportType);
    
    	//DropDownList for Month
        m_centralPanel.add(new JLabel(" Select month : "));
        m_month=new JComboBox();
			m_month.addItem("");
			m_month.addItem("January");
			m_month.addItem("February");
			m_month.addItem("March");
			m_month.addItem("April");
			m_month.addItem("May");
			m_month.addItem("June");
			m_month.addItem("July");
			m_month.addItem("August");
			m_month.addItem("Sept");
			m_month.addItem("Octomber");
			m_month.addItem("November");
			m_month.addItem("December");
		
		m_centralPanel.add(m_month);
		
		
		//DropDownList for Year
        m_centralPanel.add(new JLabel(" Select Year : "));
        m_year=new JComboBox();
		m_year.addItem("");
		for (int i=2010;i<=2020;i++)
			 m_year.addItem(""+i);
   		m_centralPanel.add(m_year);
		
   		//Create Buttons
		btn_reset=new JButton("Reset");
		btn_report=new JButton("Generate report");
		
		//Add buttons to panel
		m_leftPanel.add(btn_reset);
		m_leftPanel.add(btn_report);
		
		btn_report.addActionListener(this);
		btn_reset.addActionListener(this);
		
		
		
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

		//Make the frame half the height and width
		Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
		int height = screenSize.height;
		int width = screenSize.width;
		setSize(width/2, height/2);

		//Position the JFrame in centre
		setLocationRelativeTo(null);
		setVisible(true);

	}	
		public void actionPerformed(ActionEvent e)
		{
			if(btn_report==e.getSource())
			{
				try
				{
				//Database connection
				Class.forName("com.mysql.jdbc.Driver").newInstance();
				Connection con=DriverManager.getConnection("jdbc:mysql://localhost:3306/Attendance","root","root");
				Statement s=con.createStatement();
				System.out.println("Database connection established");

				File dir_Report,dir_WorkHr,dir_Leave;
				dir_Report = new File("d:\\sheetal\\aarnum\\Report");
				dir_WorkHr=new File("d:\\sheetal\\aarnum\\Report\\Working Hours");
				dir_Leave=new File("d:\\sheetal\\aarnum\\Report\\Leave Details");
				if(dir_Report.isDirectory()){} 
			    else
			     {
			    	 dir_Report.mkdirs();
			     } 
				if(dir_WorkHr.isDirectory()){}	
				else
				{
					dir_WorkHr.mkdirs();
				}
				if(dir_Leave.isDirectory()){}
				else
				{
					dir_Leave.mkdirs();
				}
				String leaveDetail_year,leaveDetail_month;
				File dirLeave_year,dirLeave_month;
				
				
				if((String)m_reportType.getSelectedItem()=="Working Details")
				{
					System.out.println("dsafdffs");
				//Leave Details directory containing years and months.
				//Year Directory
					leaveDetail_year  =(String)m_year.getSelectedItem();
			  		dirLeave_year=new File("d:\\sheetal\\aarnum\\Report\\Leave Details\\"+leaveDetail_year);
			  		if(dirLeave_year.isDirectory()){}
			  		else
			  			dirLeave_year.mkdirs();
			   
			   //Month Directory
			  		leaveDetail_month=(String)m_month.getSelectedItem();
			  		dirLeave_month=new File("d:\\sheetal\\aarnum\\Report\\Leave Details\\"+leaveDetail_year+"\\"+leaveDetail_month+".csv");
			  		if(dirLeave_month.exists())
			  			System.out.println("File exist");
			  		
			  		
					
				
				//*******
				//This code i want in separate function and because above code is same for both"Leave detail" and "Working Detail" folder but it didn't work
			  	//*******  
				dirLeave_month.createNewFile();
				FileWriter f1 = new FileWriter(dirLeave_month);
			
				s.executeQuery("SELECT * FROM EMP_LEAVE");
				ResultSet rset=s.getResultSet();	
				
				//Retrieve data  from tables 
				String emp_id,emp_leave;
				while(rset.next())
				{
					emp_id=rset.getString(1);
					emp_leave=rset.getString(2);
					
					f1.write(emp_id+","+emp_leave+"\n");
					
				}
				
				f1.close();
				}
				}
				
			
			
				catch(IOException e1)
				{
					System.out.println(e1.getMessage());
					e1.printStackTrace();
					return;
				}
			
				catch(ClassNotFoundException e1)
				{
					System.out.println(e1);
					return;
				}

				catch(Exception e1)
				{
					System.out.println(e1.getMessage());
					e1.printStackTrace();
					return;
				}
				
			    
		}
		
		if(btn_reset==e.getSource())
		{
			/* m_reportType.setToolTipText("");
			 m_reportType.setToolTipText("");
			 m_month.setToolTipText("");
			 m_year.setToolTipText("");*/
		}
	}
}
